package com.elive.service.impl;

import com.elive.common.ServerResponse;
import com.elive.dao.*;
import com.elive.pojo.*;
import com.elive.service.IApartmentService;
import com.elive.vo.ApartmentDeviceVo;
import com.elive.vo.ApartmentInfoVo;
import com.elive.vo.ApartmentServiceVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuyin on 2018/3/13.
 */
@Service("iApartmentService")
public class ApartmentServiceServiceImpl implements IApartmentService {

    @Autowired
    private ApartmentMapper apartmentMapper;

    @Autowired
    private ApartmentDeviceMapper apartmentDeviceMapper;

    @Autowired
    private ApartmentServiceMapper apartmentServiceMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private ApartmentLikeMapper apartmentLikeMapper;

    @Override
    public ServerResponse<PageInfo> getApartmentListByHostId(int pageNum, int pageSize, Integer hostId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Apartment> apartmentList = apartmentMapper.getApartmentListByHostId(hostId);
        if(apartmentList.size() != 0){
            PageInfo pageResult = new PageInfo(apartmentList);
            return ServerResponse.createBySuccess(pageResult);
        }else {
            return ServerResponse.createByErrorMessage("没有公寓列表信息");
        }
    }

    @Override
    public ServerResponse<String> addApartment(Apartment apartment, String[] deviceIdList, String[] serviceIdList) {
        if(apartment == null){
            return ServerResponse.createByErrorMessage("请补充完整公寓信息");
        }
        int resultCount = apartmentMapper.insertSelective(apartment);
        if(resultCount == 0){
            return ServerResponse.createByErrorMessage("添加公寓信息失败");
        }

        Integer apartmentId = apartment.getId();
        for(String deviceId : deviceIdList){
            Integer id = Integer.valueOf(deviceId);
            ApartmentDevice apartmentDevice = new ApartmentDevice();
            apartmentDevice.setApartmentId(apartmentId);
            apartmentDevice.setDeviceId(id);
            apartmentDevice.setIsProvided("1");
            int deviceCount = apartmentDeviceMapper.insertSelective(apartmentDevice);
            if(deviceCount == 0){
                return ServerResponse.createByErrorMessage("添加公寓设备信息错误");
            }

        }

        for(String serviceId : serviceIdList){
            Integer id = Integer.valueOf(serviceId);
            ApartmentService apartmentService = new ApartmentService();
            apartmentService.setApartementId(apartmentId);
            apartmentService.setServiceId(id);
            apartmentService.setIsProvided("1");
            int serviceCount = apartmentServiceMapper.insertSelective(apartmentService);
            if(serviceCount == 0){
                return ServerResponse.createByErrorMessage("添加公寓服务信息错误");
            }
        }


        return ServerResponse.createBySuccessMessage("添加公寓成功");
    }

    @Override
    public ServerResponse<ApartmentInfoVo> getApartmentInfo(Integer apartmentId) {
        // 获取公寓基本信息
        Apartment apartment = apartmentMapper.selectByPrimaryKey(apartmentId);
        if(apartment != null){

            // 所有的设备列表
            List<Device> allDeviceList = deviceMapper.selectDeviceList();
            List<ApartmentDevice> apartmentDeviceList = apartmentDeviceMapper.selectDeviceList(apartmentId);
            List<ApartmentDeviceVo> apartmentDeviceVoList = new ArrayList<ApartmentDeviceVo>();
            for(int i = 0; i < allDeviceList.size(); i++){
                Device device = allDeviceList.get(i);
                Boolean isProvide = false;
                for(int j = 0; j < apartmentDeviceList.size(); j++){
                    ApartmentDevice apartmentDevice = apartmentDeviceList.get(j);
                    // 如果设备id相同
                    if(device.getId() == apartmentDevice.getDeviceId()){
                        isProvide = true;
                        ApartmentDeviceVo apartmentDeviceVo = new ApartmentDeviceVo();
                        apartmentDeviceVo.setApartmentId(apartmentDevice.getApartmentId());
                        apartmentDeviceVo.setDeviceId(apartmentDevice.getDeviceId());
                        apartmentDeviceVo.setDeviceName(device.getDeviceName());
                        apartmentDeviceVo.setIsProvided(apartmentDevice.getIsProvided());
                        apartmentDeviceVoList.add(apartmentDeviceVo);
                        break;
                    }

                }
                if(!isProvide){
                    ApartmentDeviceVo apartmentDeviceVo = new ApartmentDeviceVo();
                    apartmentDeviceVo.setApartmentId(apartmentId);
                    apartmentDeviceVo.setDeviceId(device.getId());
                    apartmentDeviceVo.setDeviceName(device.getDeviceName());
                    apartmentDeviceVo.setIsProvided("0");
                    apartmentDeviceVoList.add(apartmentDeviceVo);
                }

            }

            // 所以服务列表
            List<com.elive.pojo.Service> allServiceList = serviceMapper.selectServiceList();
            List<ApartmentService> apartmentServiceList = apartmentServiceMapper.selectServiceList(apartmentId);
            List<ApartmentServiceVo> apartmentServiceVoList = new ArrayList<ApartmentServiceVo>();
            for(int i = 0; i < allServiceList.size(); i++){
                com.elive.pojo.Service service = allServiceList.get(i);
                Boolean isProvide = false;
                for(int j = 0; j < apartmentServiceList.size(); j++){
                    ApartmentService apartmentService = apartmentServiceList.get(j);
                    // 如果设备id相同
                    if(service.getId() == apartmentService.getServiceId()){
                        isProvide = true;
                        ApartmentServiceVo apartmentServiceVo = new ApartmentServiceVo();
                        apartmentServiceVo.setApartmentId(apartmentService.getApartementId());
                        apartmentServiceVo.setServiceId(apartmentService.getServiceId());
                        apartmentServiceVo.setServiceName(service.getServicename());
                        apartmentServiceVo.setIsProvided(apartmentService.getIsProvided());
                        apartmentServiceVoList.add(apartmentServiceVo);
                        break;
                    }

                }
                if(!isProvide){
                    ApartmentServiceVo apartmentServiceVo = new ApartmentServiceVo();
                    apartmentServiceVo.setApartmentId(apartmentId);
                    apartmentServiceVo.setServiceId(service.getId());
                    apartmentServiceVo.setServiceName(service.getServicename());
                    apartmentServiceVo.setIsProvided("0");
                    apartmentServiceVoList.add(apartmentServiceVo);
                }

            }

            ApartmentInfoVo apartmentInfoVo = new ApartmentInfoVo();
            apartmentInfoVo.setApartment(apartment);
            apartmentInfoVo.setDeviceList(apartmentDeviceVoList);
            apartmentInfoVo.setServiceList(apartmentServiceVoList);

            return ServerResponse.createBySuccess(apartmentInfoVo);

        }

        return ServerResponse.createByErrorMessage("查询公寓详情失败");
    }

    @Override
    public ServerResponse<String> editApartmentInfo(Apartment apartment, String[] deviceIdList, String[] serviceIdList) {
        // 修改公寓基本信息
        int resultCount = apartmentMapper.updateByPrimaryKeySelective(apartment);
        if(resultCount == 0){
            return ServerResponse.createByErrorMessage("修改公寓信息失败");
        }
        Integer apartmentId = apartment.getId();
        apartmentDeviceMapper.deleteByApartmentId(apartmentId);
        apartmentServiceMapper.deleteByApartmentId(apartmentId);
        for(String deviceId : deviceIdList){
            Integer id = Integer.valueOf(deviceId);
            ApartmentDevice apartmentDevice = new ApartmentDevice();
            apartmentDevice.setApartmentId(apartmentId);
            apartmentDevice.setDeviceId(id);
            apartmentDevice.setIsProvided("1");
            int deviceCount = apartmentDeviceMapper.insertSelective(apartmentDevice);
            if(deviceCount == 0){
                return ServerResponse.createByErrorMessage("修改公寓设备信息错误");
            }

        }

        for(String serviceId : serviceIdList){
            Integer id = Integer.valueOf(serviceId);
            ApartmentService apartmentService = new ApartmentService();
            apartmentService.setApartementId(apartmentId);
            apartmentService.setServiceId(id);
            apartmentService.setIsProvided("1");
            int serviceCount = apartmentServiceMapper.insertSelective(apartmentService);
            if(serviceCount == 0){
                return ServerResponse.createByErrorMessage("修改公寓服务信息错误");
            }
        }

        return ServerResponse.createBySuccessMessage("修改公寓信息成功");
    }

    @Override
    public ServerResponse<String> deleteApartment(Integer apartmentId) {
        apartmentMapper.deleteByPrimaryKey(apartmentId);
        apartmentServiceMapper.deleteByApartmentId(apartmentId);
        apartmentDeviceMapper.deleteByApartmentId(apartmentId);
        return ServerResponse.createBySuccessMessage("删除公寓信息成功");
    }

    @Override
    public ServerResponse<PageInfo> getRecommendApartmentList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Apartment> apartmentList = apartmentMapper.getRecommentApartment();
        if(apartmentList.size() > 0){
            PageInfo pageResult = new PageInfo(apartmentList);
            return ServerResponse.createBySuccess(pageResult);
        } else {
            return ServerResponse.createByErrorMessage("没有公寓列表信息");
        }
    }

    @Override
    public ServerResponse<PageInfo> getApartmentListByCityId(Integer cityId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Apartment> apartmentList = apartmentMapper.getApartmentListByCityId(cityId);

        if(apartmentList.size() > 0){
            PageInfo pageResult = new PageInfo(apartmentList);
            return ServerResponse.createBySuccess(pageResult);
        } else {
            return ServerResponse.createByErrorMessage("没有公寓列表信息");
        }
    }

    @Override
    public ServerResponse<Apartment> updateLikeCountByApartmentId(Integer apartmentId,Integer userId) {
        Apartment apartment = apartmentMapper.selectByPrimaryKey(apartmentId);
        Integer likeCount = apartment.getLikeCount();
        apartment.setLikeCount(likeCount + 1);
        int count = apartmentMapper.updateByPrimaryKeySelective(apartment);
        ApartmentLike apartmentLike = new ApartmentLike();
        apartmentLike.setApartmentId(apartmentId);
        apartmentLike.setUserId(userId);
        int countApartmentLike = apartmentLikeMapper.insertSelective(apartmentLike);
        if(count > 0 && countApartmentLike > 0){
            return ServerResponse.createBySuccess("点赞成功",apartment);
        }
        return ServerResponse.createByErrorMessage("点赞失败");
    }

    @Override
    public ServerResponse<Apartment> cancelLikeCountByApartmentId(Integer apartmentId,Integer userId) {
        Apartment apartment = apartmentMapper.selectByPrimaryKey(apartmentId);
        Integer likeCount = apartment.getLikeCount();
        apartment.setLikeCount(likeCount - 1);
        int count = apartmentMapper.updateByPrimaryKeySelective(apartment);
        int deletedCount = apartmentLikeMapper.deletedApartmentLike(apartmentId,userId);
        if(count > 0 && deletedCount > 0){
            return ServerResponse.createBySuccess("取消点赞成功",apartment);
        }
        return ServerResponse.createByErrorMessage("取消点赞失败");

    }

    @Override
    public ServerResponse<Boolean> isLiked(Integer apartmentId, Integer userId) {
        int count = apartmentLikeMapper.selectedApartmentLike(apartmentId,userId);
        if(count > 0){
            return ServerResponse.createBySuccess("成功2",true);
        }
        return ServerResponse.createBySuccess("成功3",false);
    }


}
